$(function (){

    //调用数据库呈现数据库中所有图书
    $.ajax({
        type:'GET',
        url :'editBook',
        data: {'type':1},
        success:function (data){
            console.log(data);
            var innerHTML = '';
            for( var i = 0;i<data.length;i++){
                innerHTML +='<tr class="book-data">\n' +
                    '           <td>'+data[i].bookId+'</td>\n' +
                    '           <td>'+data[i].bookName+'</td>\n' +
                    '           <td>'+data[i].bookAuthor+'</td>\n' +
                    '           <td>'+data[i].bookClass+'</td>\n' +
                    '           <td>'+data[i].bookPress+'</td>\n' +
                    '           <td>'+data[i].bookDescription+'</td>\n' +
                    '           <td>'+data[i].bookInventory+'</td>\n' +
                    '           <td class="btns-action">\n' +
                    '                <button class="action-btn" id="edit-book-btn">编辑</button>\n' +
                    '                <button class="action-btn" id="delete-book-btn">删除</button>\n' +
                    '            </td>\n' +
                    '         </tr>';
            }
            $('#book-table').append(innerHTML);
        },
        error:function (errorMsg) {
            console.log(errorMsg);
        }
    })

    var $add_form        = $('#add-form'),
        $edit_form       = $('#edit-form'),
        $btn_new         = $('#btn-new'),
        $search_input    = $('#search-input'),
        $auto_complete   = $('#auto-complete'),

        $table           = $('table');
    $add_form.hide();

    //重置功能：显示所有图书
    $(document).delegate('#btn-reset','click',function () {
        var $tr_book_data    = $('tr.book-data');
        //下拉列表选择请选择 id不能用了好奇怪
        $('.select-comboBox').val('请选择');
        $tr_book_data.show();
    })


    //筛选图书
    //作者
    $(document).delegate('#author-comboBox','change',function () {
        var $tr_book_data    = $('tr.book-data'),
            select_author    = $('#author-comboBox').val();
        console.log('选择作者:'+select_author);
        if(select_author !== '请选择'){
            //筛选作者
            $tr_book_data.hide().filter(":contains('"+select_author+"')").show();
        }else{
            //全显示
            $tr_book_data.show();
        }
    });
    //类别
    $(document).delegate('#class-comboBox','change',function () {
        var $tr_book_data    = $('tr.book-data'),
            select_class    = $('#class-comboBox option:selected').text();
        console.log('选择类别');
        if(select_class !== '请选择'){
            //筛选类别
            $tr_book_data.hide().filter(":contains('"+select_class+"')").show();
        }else{
            //全显示
            $tr_book_data.show();
        }
    });
















    //添加图书
    $btn_new.on('click',function () {
        $add_form.show();
        $btn_new.removeClass('gray');
    })
    $('#close-form-btn').on('click',function () {
        $add_form.hide();
        $btn_new.addClass('gray');
    })
    $('#confirm-add-btn').on('click',function (){
        var bookId          =$('#bookId').val(),
            bookName        =$('#bookName').val(),
            bookAuthor      =$('#bookAuthor').val(),
            bookClass       =$('#bookClass').val(),
            bookPress       =$('#bookPress').val(),
            bookDescription =$('#bookDescription').val(),
            bookInventory   =$('#bookInventory').val();
        // console.log(bookId);

        //调用数据库向数据库中添加图书
        $.ajax({
            type: 'GET',
            url: 'editBook',
            data:{'type':2,'bookId':bookId,'bookName':bookName,'bookAuthor':bookAuthor,'bookClass':bookClass,'bookPress':bookPress,'bookDescription':bookDescription,'bookInventory':bookInventory},
            success:function (data){
                console.log(data);
                if(data === 'Success'){
                    $table.append('<tr>\n' +
                        '                <td>'+bookId+'</td>\n' +
                        '                <td>'+bookName+'</td>\n' +
                        '                <td>'+bookAuthor+'</td>\n' +
                        '                <td>'+bookClass+'</td>\n' +
                        '                <td>'+bookPress+'</td>\n' +
                        '                <td>'+bookDescription+'</td>\n' +
                        '                <td>'+bookInventory+'</td>\n' +
                        '                <td class="btns-action">\n' +
                        '                    <button class="action-btn" id="edit-book">编辑</button>\n' +
                        '                    <button class="action-btn" id="delete-book">删除</button>\n' +
                        '                </td>\n' +
                        '            </tr>');
                }else{
                    confirm('添加失败，请重试');
                }
            },
            error:function (errorMsg){
                console.log(errorMsg);
            }
        })
    })
    //删除图书
    $table.delegate('#delete-book-btn','click',function () {
        var result = confirm('是否删除');
        if(result){
            var deleteRow = $(this).closest('tr');
            var deleteBookId = deleteRow.find('td:eq(0)').text();
            console.log(deleteBookId);
            $(this).closest('tr').remove();

            //TODO:数据库中删除该图书
            $.ajax({
                type:'GET',
                url:'editBook',
                data:{'type':5,'deleteBookId':deleteBookId},
                success:function (data) {
                    if(data === 'Success')
                    console.log('删除成功');
                },
                error:function (errorMsg) {
                    console.log(errorMsg);
                }
            });
        }
    });
    //编辑图书
    $table.delegate('#edit-book-btn','click',function () {
        $edit_form.show();
        $(this).addClass('gray');
        //将点击编辑的图书信息展示在form表单中
        //将选中的用户信息显示在form表单中
        var bookId = $(this).closest('tr').find('td:eq(0)').text(),
            bookName = $(this).closest('tr').find('td:eq(1)').text(),
            author = $(this).closest('tr').find('td:eq(2)').text(),
            bookClass = $(this).closest('tr').find('td:eq(3)').text(),
            press = $(this).closest('tr').find('td:eq(4)').text(),
            description = $(this).closest('tr').find('td:eq(5)').text(),
            inventory = $(this).closest('tr').find('td:eq(6)').text();
        console.log(bookName);

        $('#edit-bookId').val(bookId);
        $('#edit-bookName').val(bookName);
        $('#edit-author').val(author);
        $('#edit-class').val(bookClass);
        $('#edit-press').val(press);
        $('#edit-description').val(description);
        $('#edit-inventory').val(inventory);
    });
    //关闭编辑图书的界面
    $('#close-edit-form-btn').on('click',function () {
        $edit_form.hide();
        $('.action-btn').removeClass('gray');//把操作中的所有按钮都移除gray的类
    })
    //确认关闭的按钮
    $('#confirm-edit-btn').on('click',function () {
        var result = confirm('编辑成功！');
        if(result){
            $edit_form.hide();
            $('.action-btn').removeClass('gray');//把操作中的所有按钮都移除gray的类
        }
    });


    //查询图书
    //搜索框查询
    //自动补全功能
    $search_input.on('keyup',function () {
        var keyword = $(this).val();
        if(keyword !== '' && keyword !== null && keyword.length !== 0){
            $.ajax({
                type:'GET',
                url:'editBook',
                data:{'type':3,'keyword':keyword},
                success:function (data){
                    console.log(data);
                    var bookListHTML = '';
                    for(var i = 0;i<data.length;i++){
                        bookListHTML += '<li data-bookName="'+data[i].bookName+'">'+data[i].bookName+' : '+data[i].bookAuthor+'</li>';
                    }
                    $('#auto-complete-list').html(bookListHTML);
                    $auto_complete.show();
                },
                error:function (errorMsg){
                    console.log(errorMsg);
                }
            });
        }else{
            $auto_complete.hide();
        }
    })
    $(document).on('click','li',function (){
        $auto_complete.hide();
        var bookName = $(this).data('bookname');
        console.log(bookName);
        //在搜索框中补全书名
        $search_input.val(bookName);

        //刷新下方表格，筛出这本书
        $.ajax({
            type:'GET',
            url :'editBook',
            data: {'type':4,'liBookName':bookName},
            success:function (data){
                console.log(data);
                //清空表格
                $('.book-data').remove();
                //显示该书
                var innerHTML = '';
                innerHTML +='<tr class="book-data">\n' +
                    '           <td>'+data.bookId+'</td>\n' +
                    '           <td>'+data.bookName+'</td>\n' +
                    '           <td>'+data.bookAuthor+'</td>\n' +
                    '           <td>'+data.bookClass+'</td>\n' +
                    '           <td>'+data.bookPress+'</td>\n' +
                    '           <td>'+data.bookDescription+'</td>\n' +
                    '           <td>'+data.bookInventory+'</td>\n' +
                    '           <td class="btns-action">\n' +
                    '                <button class="action-btn" id="edit-book">编辑</button>\n' +
                    '                <button class="action-btn" id="delete-book">删除</button>\n' +
                    '            </td>\n' +
                    '         </tr>';
                $('#book-table').append(innerHTML);
            },
            error:function (errorMsg) {
                console.log(errorMsg);
            }
        })
    });
    // //搜索框失去焦点隐藏自动补全框
    // $search_input.on('blur',function () {
    //     $auto_complete.hide();
    // });
    //点击搜索按钮
    $('#search-btn').on('click',function(){
        $auto_complete.hide();
        var keyword =$search_input.val();
        console.log(keyword);
        //刷新下方表格，筛出这些书
        $.ajax({
            type:'GET',
            url :'editBook',
            data: {'type':3,'keyword':keyword},
            success:function (data){
                console.log(data);
                //清空表格
                $('.book-data').remove();
                //显示这些书
                var innerHTML = '';
                for(var i = 0;i<data.length;i++){
                    innerHTML +='<tr class="book-data">\n' +
                        '           <td>'+data[i].bookId+'</td>\n' +
                        '           <td>'+data[i].bookName+'</td>\n' +
                        '           <td>'+data[i].bookAuthor+'</td>\n' +
                        '           <td>'+data[i].bookClass+'</td>\n' +
                        '           <td>'+data[i].bookPress+'</td>\n' +
                        '           <td>'+data[i].bookDescription+'</td>\n' +
                        '           <td>'+data[i].bookInventory+'</td>\n' +
                        '           <td class="btns-action">\n' +
                        '                <button class="action-btn" id="edit-book">编辑</button>\n' +
                        '                <button class="action-btn" id="delete-book">删除</button>\n' +
                        '            </td>\n' +
                        '         </tr>';
                }

                $('#book-table').append(innerHTML);
            },
            error:function (errorMsg) {
                console.log(errorMsg);
            }
        })
    });




})